package com.lanxin.qx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lanxin.qx.entity.QxRole;
import com.lanxin.qx.mapper.QxRoleMapper;
import com.lanxin.qx.service.IQxRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyq
 * @since 2025-08-01
 */
@Service
public class QxRoleServiceImpl extends ServiceImpl<QxRoleMapper, QxRole> implements IQxRoleService {

    @Override
    public IPage<QxRole> getQxRoleByPage(Integer page, Integer pageSize, String roleName) {
        Page<QxRole> pageParam = new Page<>(page, pageSize);
        QueryWrapper<QxRole> queryWrapper = new QueryWrapper<>();
        if (StringUtils.hasText(roleName)) {
            queryWrapper.like("roleName", roleName); // 模糊搜索逻辑
        }
        return this.page(pageParam, queryWrapper); // 调用父类方法查询
    }

    @Override
    public boolean checkRoleName(String roleName, Integer oid) {
        QueryWrapper<QxRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("roleName", roleName); // 校验名称是否存在
        if (oid != null) {
            queryWrapper.ne("oid", oid); // 排除自身（修改时）
        }
        return this.count(queryWrapper) > 0; // 返回是否存在
    }

}
